package org.opsli.modulars.system.appValve.web;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.opsli.api.wrapper.system.user.UserModel;
import org.opsli.common.annotation.ApiRestController;
import org.opsli.common.api.Result;
import org.opsli.core.base.controller.BaseRestController;
import org.opsli.core.utils.JwtUtil;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.system.appValve.entity.SysLoginModel;
import org.opsli.modulars.system.appValve.service.AppValveInterface;
import org.opsli.modulars.system.user.entity.SysUser;
import org.opsli.modulars.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags="用户登录")
@Slf4j
@ApiRestController("/appValveSys")
public class AppValveController extends BaseRestController<SysUser, UserModel, IUserService>{

    @Autowired
    private AppValveInterface appInterface;
    @Autowired
    private IUserService userService;


    @GetMapping(value = "/dList")
    public List<Map> dList(HttpServletRequest request) throws Exception{
        Map datamap=new HashMap();
        List<Map> list=new ArrayList<Map>();
        String userName = JwtUtil.getUserNameByToken(request);
        UserModel user = UserUtil.getAppUser(userName);
        List<SysUser> models =userService.getList(user.getCompanyId());
        if(models!=null&&models.size()>0){
            for(SysUser model:models){
                Map map=new HashMap();
                map.put("text",model.getRealName());
                map.put("value",model.getId());
                list.add(map);
            }
        }
        return list;
    }

    /**
     * app登录
     * @param sysLoginModel
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/mLogin", method = RequestMethod.POST)
    public Result mLogin(@RequestBody SysLoginModel sysLoginModel) throws Exception {
        Result result;
        String username = sysLoginModel.getUsername();
        String password = sysLoginModel.getPassword();

        //1. 校验用户是否有效
        JSONObject obj = new JSONObject();
        UserModel userModel = IService.queryByUserName(username);
        result = appInterface.checkUserIsEffective(userModel);
        if(!result.isSuccess()) {
            return result;
        }

        //2. 校验用户名或密码是否正确
        String userpassword = UserUtil.handlePassword(password, userModel.getSecretKey());
        String syspassword = userModel.getPassword();
        if (!syspassword.equals(userpassword)) {
            result.error500("用户名或密码错误!!!");
            return result;
        }
        //用户登录信息
        obj.put("userInfo", userModel);

        // 生成token
        String token = JwtUtil.sign(username, syspassword);
        //token 信息
        obj.put("token", token);
        result.setResult(obj);
        result.setSuccess(true);
        result.setCode(200);
        return result;
    }

    @RequestMapping(value = "/saveClientId", method = RequestMethod.GET)
    public Result<SysUser> saveClientId(HttpServletRequest request, @RequestParam("clientId")String clientId) {
        Result<SysUser> result = new Result<SysUser>();
        result.setSuccess(true);
        result.setCode(200);
        return result;
    }

}
